package com.zcxy360.sandtable.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zcxy360.sandtable.domain.Course;
import com.zcxy360.sandtable.service.CourseService;

/**
 * 课程接口
 * @author 盛亚辉
 *
 */
@RequestMapping("course")
@RestController
public class CourseController {

	@Autowired
	private CourseService courseService;

	/**
	 * 获取课程
	 * @param courseId 课程id
	 * @return 课程对象
	 */
	@RequiresAuthentication
	@GetMapping("{courseId}")
	public Object getByCourseId(@PathVariable("courseId") Integer courseId) {
		return courseService.getByCourseId(courseId);
	} 

	/**
	 * 获取课程集合
	 * @param course 课程字段
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 课程集合
	 */
	@RequiresAuthentication
	@GetMapping
	public Object list(Course course, String key,String orderByClause,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return courseService.list(course, key, orderByClause,pageNum, pageSize, navigatePages);
	}
	
	/**
	 * 获取日历课程集合
	 * @param date 日期
	 * @param orderByClause 排序规则
	 * @return 课程集合
	 * @throws ParseException 日期解析异常
	 */
	@RequiresAuthentication
	@GetMapping("calendar")
	public Object listForCalendar(@RequestParam("date") String date,String orderByClause) throws ParseException {
		return courseService.listForCalendar(new SimpleDateFormat("yyyy-MM-dd").parse(date),orderByClause);
	}
	
	/**
	 * 获取可加入课程集合
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @return 课程集合
	 */
	@RequiresRoles(value="student")
	@GetMapping("join")
	public Object listForJoin(String key,String orderByClause) {
		return courseService.listForJoin(key,orderByClause);
	}
	
	/**
	 * 获取可学习的课程集合
	 * @param type 课程类型
	 * @param orderByClause 排序规则
	 * @return 课程集合
	 */
	@RequiresRoles(value="student")
	@GetMapping("study")
	public Object listForStudy(@RequestParam("type") Integer type,String orderByClause) {
		return courseService.listForStudy(type,orderByClause);
	}

	/**
	 * 新增课程
	 * @param course 课程字段
	 * @param stageJson 课程阶段json字符串
	 * @param unitJson 阶段单元json字符串
	 * @param file 导入用户文件
	 * @return 课程对象
	 */
	@RequiresRoles(value="teacher")
	@PostMapping
	public Object post(Course course,String stageJson,String unitJson,MultipartFile file) {
		return courseService.post(course,stageJson,unitJson,file);
	}
	
	/**
	 * 克隆课程
	 * @param course 课程字段
	 * @param file 导入用户文件
	 * @return 布尔值
	 */
	@RequiresRoles(value="teacher")
	@PostMapping("clone")
	public Object clone(Course course,MultipartFile file) {
		return courseService.clone(course,file);
	}
	
	/**
	 * 更新课程
	 * @param course 课程字段
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PutMapping
	public Object update(Course course) {
		return courseService.update(course);
	}
	
	/**
	 * 删除课程
	 * @param courseIds 课程id数组
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","manager","teacher"},logical=Logical.OR)
	@DeleteMapping("{courseIds}")
	public Object delete(@PathVariable("courseIds") int[] courseIds) {
		return courseService.delete(courseIds);
	}
	
	/**
	 * 检查名称
	 * @param name 课程名称
	 * @return 布尔值
	 */
	@RequiresRoles(value="teacher")
	@GetMapping("checkName")
	public Object checkName(@RequestParam("name")String name) {
		return courseService.checkName(name);
	}
	
	/**
	 * 结束课程
	 * @param courseId 课程id
	 * @return 布尔值
	 */
	@RequiresRoles(value={"admin","teacher"},logical=Logical.OR)
	@PutMapping("end")
	public Object end(@RequestParam("courseId") Integer courseId) {
		return courseService.end(courseId);
	}
	
	
}
